******************************************************
******** This file creates the files $dir3\county_supply_old.dta and $dir3\county_supply_new.dta 
**** input file:  $dir1\Plant_Level_Runs_070110.dta  (old optimization output)
**** input files: $dir1\results_baseline_links_cleaned.dta", clear   (new optimization output)
**** input file:  $dir1\run.dta  (relates "runs" to "prices")
**** input file:  $dir1\CornYields_clean.dta
**** input file:  $dir1\HECYields_clean.dta


clear matrix
drop _all
set mem 500m


***** user:  sph is 1;  jeh is 2; ck is 3
global user=1    
****   toggle:  $toggle==2 (corrects trapezoids) $toggle==1 (shifts supply out) $toggle==0 (no correction) 
global toggle=2
global retail_tax_adj=0.63
***********  Note:  verify that run=="run2" is first run and run=="run28" is last run 

***sph's directories
if $user==1{
global dir1="C:\AllStephen\My Dropbox\Jon n Chris\1 - Supply Curves\"
global dir2="C:\AllStephen\My Dropbox\Jon n Chris\2 - Policy Simulation\Simulation Results"
global dir3="C:\AllStephen\My Dropbox\Jon n Chris\3 - Dist of Gains\"
global dir4="C:\AllStephen\My Dropbox\Jon n Chris\4 - Land Area Calcs\"
global dir5="C:\AllStephen\My Dropbox\Jon n Chris\5 - Probits\Data\"
}

***jeh's directories
if $user==2 {
global dir1="/Users/Jon/Dropbox/LandUse/1 - Supply Curves/"
global dir2="/Users/Jon/Dropbox/LandUse/2 - Policy Simulation/Simulation Results/"
global dir3="/Users/Jon/Dropbox/LandUse/3 - Dist of Gains/"
global dir4="/Users/Jon/Dropbox/LandUse/4 - Land Area Calcs/"
global dir5="/Users/Jon/Dropbox/LandUse/5 - Probits/Data/"
}


*************
*** Create temp1.dta with "factors"
*************
cd "$dir1"
*use "$dir1/Acres_All_Runs_070110.dta", clear  (this file no longer exists??)
*** Calculate "factor" for type == "corngrain" 
***  New file from Nathan, doesn't have type =="wet_mill" |type=="dry_mill"
*use "$dir1\results_baseline_links_cleaned.dta", clear
use "Plant_Level_Runs_070110.dta", clear
keep if type =="wet_mill" |type=="dry_mill"
keep run source_id type quant_tons 
duplicates drop run source_id type , force
rename source_id  dest_id
rename type dest_type
g factor=0.0893*0.657*(dest_type=="wet_mill")+0.1*0.657*(dest_type=="dry_mill")
*** if dry & wet mills at destination, factor is average factor
collapse (mean) factor [w=quant_tons], by (run dest_id)
g type="corngrain"
cd "$dir1"
save temp1.dta, replace


********************************************
**********  Create $dir3\county_supply_old.dta
********************************************
cd "$dir1"
*
*use "results_baseline_links_cleaned.dta", clear  
use "Plant_Level_Runs_070110.dta", clear
rename source_id fips
cd "$dir1"
merge m:1 fips using "CornYields_clean.dta"
drop if _merge==2
replace corn_yield =. if type !="corngrain"
drop _merge
merge m:1 fips using "HECYields_clean.dta"
replace hec_yield =. if type !="hec"
drop if _merge==2
drop _merge
* Base yield of 7.5 tons/acre with upper and lower scenarios 5 & 10 
gen pulpwood_yield = 7.5  if type == "pulpwood"
rename fips source_id
gen acres_hec = quant_tons/ hec_yield 
gen acres_corn = quant_tons/ corn_yield 
gen acres_pulpwood = quant_tons/ pulpwood_yield 
drop corn_yield hec_yield pulpwood_yield
*
g ethtype= "corn" if type == "corngrain" 
replace ethtype="agres" if type == "ag_res" 
replace ethtype="orch" if type == "ovw"
replace ethtype="forest" if type == "forest" || type == "pulpwood"
replace ethtype="msw" if type == "msw_dirty" || type == "msw_paper" ||type == "msw_wood" ||type == "msw_yard" 
replace ethtype="msw_food" if type == "msw_food"
replace ethtype="hec" if type == "hec"
keep if ethtype !=""
*
merge m:1 run dest_id type  using "temp1.dta"
drop if _merge==2
drop _merge
*
**** these factors from Results_new_JH.xlsx  
***  "msw_dirty" has factor=0 since cannot be used for ethanol
***   "corngrain" factors computed above
*g factor = 0.657 if type == "corngrain" 
replace factor=0.05171 if type == "ag_res" 
replace factor=0.05598 if type == "ovw"
replace factor=0.05926 if type == "forest" || type == "pulpwood"
replace factor=0 if type == "msw_dirty" 
replace factor= 0.0565 if type == "msw_paper" 
replace factor= 0.05598 if type == "msw_wood" 
replace factor= 0.04599 if type == "msw_yard" 
replace factor= 0.0509175 if type == "msw_food"
replace factor=0.05085 if type == "hec"
replace factor=factor*1000
g ethanol=factor*quant_tons
* 
cd "$dir1"
merge m:1 run using run.dta, nogen
collapse (sum) ethanol quant_tons acres_*, by (run price ethtype source_id)
****the next command should generate our supply curves in Results_new_JH.xlsx   (It does mostly!!)
*collapse (sum) ethanol, by (run price ethtype)
*sort ethtype price
*
**** Are county supply curves upward sloping???
sort ethtype source_id price
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
sum ethanol
if $toggle==1 {
replace ethanol=round(ethanol,1000)
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
*list if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1], sepby(source_id)
** should replace 1 obs ***
replace ethanol=261.1 if ethtype=="msw_food" & price==2 & source_id=="M4858016"
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
*** This will fix all the backward sloping supplies for corn
replace ethanol=ethanol[_n-1] if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1] & ethtype=="corn"
}
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
*
*** these would be a more systematic way of fixing supply curves for ethanol (need more work)
*replace ethanol=2062.1 if source_id=="S13019" & ethtype=="corn"
*replace ethanol=73000  if source_id=="S17113" & ethtype=="corn" & price>2.65
*replace ethanol=41000  if source_id=="S17123" & ethtype=="corn" & price>2.65
*replace ethanol=41000  if source_id=="S17143" & ethtype=="corn" & price>2.65
*replace ethanol=41000  if source_id=="S17175" & ethtype=="corn" & price>2.65
*replace ethanol=59000  if source_id=="S17179" & ethtype=="corn" & price>2.65
*replace ethanol=62000  if source_id=="S17203" & ethtype=="corn" & price>2.65
*replace ethanol=70000  if source_id=="S19011" & ethtype=="corn" & price>2.65
*replace ethanol=49200  if source_id=="S19013" & ethtype=="corn" & price>2.65
*replace ethanol=65000  if source_id=="S19019" & ethtype=="corn" & price>2.65
*replace ethanol=62000  if source_id=="S19031" & ethtype=="corn" & price>2.65
*replace ethanol=65000  if source_id=="S19055" & ethtype=="corn" & price>2.65
*replace ethanol=44000  if source_id=="S19095" & ethtype=="corn" & price>2.65
*replace ethanol=38500  if source_id=="S19103" & ethtype=="corn" & price>2.65
*replace ethanol=38500  if source_id=="S19105" & ethtype=="corn" & price>2.65
**** there are more of these to do!!
*replace ethanol= if source_id=="" & ethtype=="corn"
*replace ethanol= if source_id=="" & ethtype=="corn"
*
drop price
fillin run source_id ethtype 
foreach X in ethanol quant_tons acres_hec acres_corn acres_pulpwood {
replace `X'=0 if `X' ==.
}
if $toggle==1 {
*** should make two changes
replace ethanol=112000 if source_id =="S13197" & ethtype=="corn" & inlist(run, "run15","run16")
}
merge m:1 run using run.dta, nogen
sort ethtype source_id price
by ethtype source_id: egen min=min(_fillin)
drop if ethanol ==0 & min>0
drop min
tab run
replace price= price+0.63
*keep if ethtype=="hec"
*keep if source_id=="S38015"
sort ethtype source_id price
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
g cost=0
replace cost =0.5*($retail_tax_adj+price)*ethanol  if run=="run2"
replace cost=cost[_n-1]+0.5*(price +price[_n-1])*(ethanol-ethanol[_n-1]) if source_id ==source_id[_n-1]
*
if $toggle==2 {
replace cost=max(0,cost[_n-1]+0.5*(price*(ethanol>ethanol[_n-1])+price[_n-2]*(ethanol<ethanol[_n-1]) +price[_n-1])*(ethanol-ethanol[_n-1])) if source_id ==source_id[_n-1]
}
move price ethanol
drop _fillin
count if (source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]) | ( source_id ==source_id[_n+1] & ethanol >ethanol[_n+1])
*br if (source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]) | ( source_id ==source_id[_n+1] & ethanol >ethanol[_n+1])
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
cd "$dir3"
save "county_supply_old_$toggle.dta", replace

tabstat ethanol, by(ethtype) s(mean n min max)
tabstat cost, by(ethtype) s(mean n min max)



********************************************
**********  Create $dir3\county_supply_new.dta
********************************************
cd "$dir1"
*
use "results_baseline_links_cleaned.dta", clear  
*use "Plant_Level_Runs_070110.dta", clear
rename source_id fips
cd "$dir1"
merge m:1 fips using "CornYields_clean.dta"
drop if _merge==2
replace corn_yield =. if type !="corngrain"
drop _merge
merge m:1 fips using "HECYields_clean.dta"
replace hec_yield =. if type !="hec"
drop if _merge==2
drop _merge
* Base yield of 7.5 tons/acre with upper and lower scenarios 5 & 10 
gen pulpwood_yield = 7.5  if type == "pulpwood"
rename fips source_id
gen acres_hec = quant_tons/ hec_yield 
gen acres_corn = quant_tons/ corn_yield 
gen acres_pulpwood = quant_tons/ pulpwood_yield 
drop corn_yield hec_yield pulpwood_yield
*
g ethtype= "corn" if type == "corngrain" 
replace ethtype="agres" if type == "ag_res" 
replace ethtype="orch" if type == "ovw"
replace ethtype="forest" if type == "forest" || type == "pulpwood"
replace ethtype="msw" if type == "msw_dirty" || type == "msw_paper" ||type == "msw_wood" ||type == "msw_yard" 
replace ethtype="msw_food" if type == "msw_food"
replace ethtype="hec" if type == "hec"
keep if ethtype !=""
*
merge m:1 run dest_id type  using "temp1.dta"
drop if _merge==2
drop _merge
*
**** these factors from Results_new_JH.xlsx  
***  "msw_dirty" has factor=0 since cannot be used for ethanol
***   "corngrain" factors computed above
*g factor = 0.657 if type == "corngrain" 
replace factor=0.05171 if type == "ag_res" 
replace factor=0.05598 if type == "ovw"
replace factor=0.05926 if type == "forest" || type == "pulpwood"
replace factor=0 if type == "msw_dirty" 
replace factor= 0.0565 if type == "msw_paper" 
replace factor= 0.05598 if type == "msw_wood" 
replace factor= 0.04599 if type == "msw_yard" 
replace factor= 0.0509175 if type == "msw_food"
replace factor=0.05085 if type == "hec"
replace factor=factor*1000
g ethanol=factor*quant_tons
* 
cd "$dir1"
merge m:1 run using run.dta, nogen
collapse (sum) ethanol quant_tons acres_*, by (run price ethtype source_id)
****the next command should generate our supply curves in Results_new_JH.xlsx   (It does mostly!!)
*collapse (sum) ethanol, by (run price ethtype)
*sort ethtype price
*
**** Are county supply curves upward sloping???
sort ethtype source_id price
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
sum ethanol if ethanol>0
if $toggle==1{
replace ethanol=round(ethanol, 1000)
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
tab ethtype if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
*list if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1], sepby(source_id)
*** This will fix all the backward sloping supplies for corn
replace ethanol=ethanol[_n-1] if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1] & ethtype=="corn"
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
*** This will fix remaining six the backward sloping supplies 
replace ethanol=ethanol[_n-1] if ethanol <ethanol[_n-1] & source_id =="S53045" & ethtype=="forest"
replace ethanol=ethanol[_n-1] if ethanol <ethanol[_n-1] & source_id =="S53067" & ethtype=="forest"
replace ethanol=ethanol[_n-1] if ethanol <ethanol[_n-1] & source_id =="M0613210" & ethtype=="msw"
replace ethanol=ethanol[_n-1] if ethanol <ethanol[_n-1] & source_id =="M0636000" & ethtype=="msw"
replace ethanol=ethanol[_n-1] if ethanol <ethanol[_n-1] & source_id =="M0653896" & ethtype=="msw"
replace ethanol=ethanol[_n-1] if ethanol <ethanol[_n-1] & source_id =="M0681344" & ethtype=="msw"
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
}
*
drop price
fillin run source_id ethtype 
foreach X in ethanol quant_tons acres_hec acres_corn acres_pulpwood {
replace `X'=0 if `X' ==.
}
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
if $toggle==1 {
*** should make two changes
replace ethanol=112000 if source_id =="S13197" & ethtype=="corn" & inlist(run, "run15","run27","run28")
replace ethanol=1320000 if source_id =="S55011" & ethtype=="hec" & inlist(run, "run17")
}
merge m:1 run using run.dta, nogen
sort ethtype source_id price
by ethtype source_id: egen min=min(_fillin)
drop if ethanol ==0 & min>0
drop min
tab run
replace price= price+0.63
*keep if ethtype=="hec"
*keep if source_id=="S38015"
sort ethtype source_id price
count if source_id ==source_id[_n-1] & ethanol <ethanol[_n-1]
g cost=0
replace cost =0.5*($retail_tax_adj+price)*ethanol  if run=="run2"
replace cost=cost[_n-1]+0.5*(price +price[_n-1])*(ethanol-ethanol[_n-1]) if source_id ==source_id[_n-1]
*
if $toggle==2 {
replace cost=max(0,cost[_n-1]+0.5*(price*(ethanol>ethanol[_n-1])+price[_n-2]*(ethanol<ethanol[_n-1]) +price[_n-1])*(ethanol-ethanol[_n-1])) if source_id ==source_id[_n-1]
}
move price ethanol
drop _fillin
cd "$dir3"
save "county_supply_new_$toggle.dta", replace

tabstat ethanol, by(ethtype) s(mean n min max)
tabstat cost, by(ethtype) s(mean n min max)

*br if source_id ==source_id[_n-1] & cost<cost[_n-1] 
